//
//  MyAd.m
//  cooba-iOS
//
//  Created by 郭四虎 on 16/3/10.
//  Copyright © 2016年 YouKu. All rights reserved.
//

#import "MyAd.h"
#import <AdSupport/AdSupport.h>

@implementation MyAd

+(instancetype)sharedMyad
{
    static MyAd *ad = nil;
    if (!ad) {
        ad = [[self alloc]init];
    }
    
    return ad;
}

+ (void)collectShowAd:(BannerModel *)model
{
    model.event_type = [NSString stringWithFormat:@"%d",eventtype_show];
    
    if (![[self sharedMyad]checkModelisIn:model]) {
        
        [[MyAd sharedMyad]addicwithModel:model];
    }
}

- (BOOL)checkModelisIn:(BannerModel *)model
{
    for (NSDictionary *dic in self.adarr) {
        if ([[dic objectForKey:@"bannerId"]isEqualToString:model.bannerid]) {
            return YES;
            break;
        }
    }
    return NO;
}

+ (void)collectClickAd:(BannerModel *)model
{
    
    model.event_type = [NSString stringWithFormat:@"%d",eventtype_click];
    
    [[MyAd sharedMyad]addicwithModel:model];
}

- (void)claerArr
{
    
    [self.adarr removeAllObjects];
}

- (NSMutableArray *)adarr
{
    if (!_adarr) {
        _adarr = [NSMutableArray array];
    }
    return _adarr;
}

- (NSMutableDictionary *)createDic
{
    NSString *loginid;
    NSString *username;
    if (([Myuser sharedUser].name)&&USERID) {
        
        loginid = USERID;
        username = [Myuser sharedUser].name;
    }else{
        
        username = @"";
        loginid = @"";
    }
    
    if (!LONGITUDE) {
        [[NSUserDefaults standardUserDefaults]setObject:@(0) forKey:@"longitude"];
        [[NSUserDefaults standardUserDefaults]setObject:@(0) forKey:@"latitude"];
    }
    
    NSString *mac;
    if (MAC) {
        mac = MAC;
    }else{
        mac = @"";
    }
    
    NSLog(@"---------------%@ ",[Myuser getIPAddress]);
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"mac":mac,@"ip":[Myuser getIPAddress],@"clientOs":@"IOS",@"clientType":@([[[UIDevice currentDevice] systemVersion] floatValue]),@"loginId":loginid,@"userName":username,@"netType":[NSString stringWithFormat:@"%d",![ISBUS boolValue]],@"xLine":LATITUDE,@"yLine":LONGITUDE,@"happenTime":[MyDateformater getCurrentDate],@"platform":@(0)}];
    
    return dic;
}

- (void)addicwithModel:(BannerModel *)model
{
    //    NSString *adId =[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    //    NSString *uuid =[[NSUUID UUID] UUIDString];
    //    NSLog(@"%@+++%@",adId,uuid);
    
    NSMutableDictionary *dic = [self createDic];
    
    NSLog(@"%@",[MyDateformater getCurrentDate]);
    
    [dic setObject:model.bannerid forKey:@"bannerId"];
    [dic setObject:model.name forKey:@"name"];
    [dic setObject:model.path forKey:@"path"];
    [dic setObject:model.pay_type forKey:@"payType"];
    
    [dic setObject:model.event_type forKey:@"eventType"];
    
    [self.adarr addObject:dic];
}

- (void)post
{
    
    NSDictionary *parameter = @{@"statisticsList":self.adarr};

    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:&parseError];
    
    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSDictionary *par = @{@"statisticsList":str};
    
    NSLog(@"%@",str);
    [MyAFN POST:BANNERSTATISTICS parameters:par block:^(id responseObject) {
        
        NSDictionary *response = responseObject;
        if ([[response objectForKey:@"status"]integerValue]) {
    
            NSLog(@"统计发送成功");
            [self claerArr];
        }else{
            NSLog(@"%@",[response objectForKey:@"msg"]);
        }
        
    } error:^(NSError *error) {
        
        NSLog(@"统计失败");
    } fromController:nil];
}

- (void)headerViewwithTag:(NSString *)tag target:(UIViewController *)target back:(void (^)(UIView *,NSArray *dataArr,bool iserror))back
{
    [self getDataWithTag:tag back:^(NSDictionary *dic) {
        
        NSMutableArray *arr = [self analyzeData:dic];
        SDCycleScrollView *scroll = [self createHeaderview:arr target:target];
        
        back(scroll,arr,NO);
        
    } error:^(NSError *error,NSDictionary *di) {
        
        NSMutableArray *arr = [self analyzeData:di];
        SDCycleScrollView *scroll = [self createHeaderview:arr target:target];
        back(scroll,arr,YES);
    }];
    
    
}
- (void)getDataWithTag:(NSString *)tag back:(void (^)(NSDictionary *))back error:(void (^)(NSError *,NSDictionary *di))err
{
    NSDictionary *parameter;
    NSString *urlstr;
    if ([ISBUS boolValue]) {
        
        parameter = @{@"mac":MAC,@"now_time":[MyDateformater getDateString:[NSDate date]],@"tag":tag};
        urlstr = [NSString stringWithFormat:@"%@/banner",CAR_URL];
    }else{
        parameter = @{@"mac":@"",@"now_time":[MyDateformater getDateString:[NSDate date]],@"tag":tag};
        urlstr = BANNER;
    }
    
    [MyAFN POST:urlstr parameters:parameter block:^(id responseObject) {
        
        [[NSUserDefaults standardUserDefaults]setObject:responseObject forKey:[NSString stringWithFormat:@"%@adarr",tag]];
        
        back(responseObject);
    } error:^(NSError *error) {
        
        NSDictionary *dic;
        if ([[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@"%@adarr",tag]]) {
            dic = [[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@"%@adarr",tag]];
        }
        err(error,dic);
    } fromController:nil];
}

- (NSMutableArray *)analyzeData:(NSDictionary *)data
{
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dic in [data objectForKey:@"bannerList"]) {
        
        BannerModel *model = [[BannerModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        
        if (model.path.integerValue == 0) {
            
        }else if((model.path.integerValue >0)&(model.path.integerValue<=5)){
            [arr addObject:model];
        }
    }
    return arr;
}

- (SDCycleScrollView *)createHeaderview:(NSArray *)arr target:(id)target
{
    SDCycleScrollView *scrollView;
    if (arr.count) {
        NSMutableArray *temArr = [NSMutableArray array];
        for (BannerModel *model in arr) {
            NSString *url;
            if ([ISBUS boolValue]) {
                url = [NSString stringWithFormat:@"%@/%@",CAR_URL,model.home_pic];
            }else{
                url = model.home_pic;
            }
            
            //url = [NSString stringWithFormat:@"http://192.168.2.134:1542%@",model.home_pic];
            [temArr addObject:url];
        }
        
        scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 65, WIDTH, SCALEY*(179)) imageURLStringsGroup:temArr];
        
    }else{
        
//        arr=@[[UIImage imageNamed:@"APP加载页_meitu_4_meitu_1.jpg"]];
//        scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, SCALEY*(179)) imagesGroup:arr];
    }
    
    if (arr.count == 1) {
        scrollView.autoScroll = NO;
        scrollView.infiniteLoop = NO;
    }
    
//    self.collectAd(self.scrollView,_adarr,_isError);
    //设置需要的属性
    //view2.showPageControl = NO;
    scrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    scrollView.autoScrollTimeInterval = 5;
    scrollView.delegate = target;
    // scrollView.titlesGroup = strArr;
    scrollView.dotColor = [UIColor whiteColor];
    //scrollView.titleLabelHeight = 50;
    
    return scrollView;
}


@end
